﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabMeasurement_Detail.xaml
    /// </summary>
    public partial class VMTabMeasurement_Detail : UserControl
    {
        public VMTabMeasurement_Detail()
        {
            InitializeComponent();
            SetupEvent();
        }
        Step_Measurement_Item _Step_Measurement_Item_Org;
        public void SetupDataContext(Step_Measurement_Item data)
        {
            //if (data == null)
            //{
            //    MessageBox.Show("data == null. Xin thông báo tới chúng tôi để sửa lỗi");
            //    return;
            //}
            _Step_Measurement_Item_Org = data;
            Step_Measurement_Item newData = data == null? new Step_Measurement_Item(): CloneHelper.CloneHelper.CloneWithSerializer<Step_Measurement_Item>(data);
            this.DataContext = newData;
        }
        private void SetupEvent()
        {
            Button_OK.Click += new RoutedEventHandler(Button_OK_Click);
            Button_Cancel.Click += new RoutedEventHandler(Button_Cancel_Click);
        }
        public Action<Step_Measurement_Item> Action_AddNew;
        void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            Step_Measurement_Item current = this.DataContext as Step_Measurement_Item;
            if (current == null)
            {
                MessageBox.Show("current == null. Xin thông báo tới chúng tôi để sửa lỗi");
            }
            if (_Step_Measurement_Item_Org != null)
            {
                if (_Step_Measurement_Item_Org == null)
                {
                    MessageBox.Show("_Step_Measurement_Item_Org == null. Xin thông báo tới chúng tôi để sửa lỗi");
                }               
                //Copy
                var props = typeof(Step_Measurement_Item).GetProperties();
                foreach (var p in props)
                {
                    if (p.CanRead && p.CanWrite)
                    {
                        var v = p.GetValue(current, null);
                        p.SetValue(_Step_Measurement_Item_Org, v, null);
                    }
                }
            }
            else
            {
                if (Action_AddNew != null)
                {
                    Action_AddNew(current);
                }
            }
            Close();
        }

        void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {

            Close();
        }

        private void Close()
        {
            var w = DependencyHelper.GetAncestorByTypeOrBaseType(this, typeof(Window)) as Window;
            if (w != null)
                w.Close();
        }
    }
}
